package com.own.core.object.admin;

import java.util.Date;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.own.core.html.form.validateType;
import com.own.core.html.form.checkBox.CheckBox;
import com.own.core.html.form.dateField.DateField;
import com.own.core.html.form.dropDown.DropDown;
import com.own.core.html.form.object.ObjectField;
import com.own.core.html.form.password.Password;
import com.own.core.html.form.textArea.TextArea;
import com.own.core.html.form.textField.TextField;
import com.own.core.object.Dao;
import com.own.core.object.admin.Group;
import com.own.core.object.lookup.Gender;
import com.own.core.object.lookup.address.City;
import com.own.core.object.lookup.address.Country;
import com.own.core.util.OwnDate;

@Entity
@Table(name = "core_user")

@NamedQuery(name = "authorizedUser", query = " from User user where user.userName =:userName and  user.password =:password  ")
public class User extends Dao {

	private long id;

	@TextField(required = true, list = true)
	private String userName;

	@Password(required = true)
	private String password;

	@TextField(required = true, validate = validateType.ALPHABETIC)
	private String firstName;

	@TextField(validate = validateType.ALPHABETIC)
	private String middleName;

	@TextField(required = true, validate = validateType.ALPHABETIC)
	private String lastName;

	@DropDown(effectOn = "city",list=false, autoComplete = true)
	private Country country;

	@DropDown(lazy = true, list=false, autoComplete = true)
	private City city;

	@DateField(list=false)
	private OwnDate birthDate;

	@TextArea
	private String address;

	@TextField(required = true, validate = validateType.EMAIL)
	private String email;

	@TextField(validate = validateType.NUMERIC)
	private String phone;

	@DropDown(property="displayName")
	private Gender gender;

	@CheckBox
	private boolean active;
	
	@ObjectField (iconSupport=true)
	private Set<Group> groups;

	
	
	public User(){}
	
	public User(String userName,String password){
		
		this.userName=userName;
		this.password=password;
	}
	
	
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	public long getId() {
		return id;
	}

	@ManyToOne
	@JoinColumn(name = "country_id", nullable = true)
	public Country getCountry() {
		return country;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "city_id", nullable = true)
	public City getCity() {
		return city;
	}

	public Gender getGender() {
		return gender;
	}
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "user_group",
			joinColumns = { @JoinColumn(name = "user_id", referencedColumnName="id") },
			inverseJoinColumns = { @JoinColumn(name = "group_id", referencedColumnName="id") })
	public Set<Group> getGroups() {
		return groups;
	}

	@Transient
	public String getName() {

		return firstName + " " + (middleName == null ? "" : (middleName + " "))
				+ lastName;
	}

	// @Column(columnDefinition="DATE" , )

	@Basic
	@Temporal(TemporalType.DATE)
	public Date getBirthDate() {
		return birthDate;
	}

	public String getAddress() {
		return address;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}

	public String getEmail() {
		return email;
	}

	public String getPhone() {
		return phone;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public void setBirthDate(OwnDate birthDate) {
		this.birthDate = birthDate;
	}

	public void setBirthDate(Date birthDate) {
		if (birthDate == null) {
			birthDate = null;

		} else {
			this.birthDate = new OwnDate(birthDate);
		}
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}

}
